//
//  CustomTextView.m
//  PinZhe
//
//  Created by Sun on 2019/4/28.
//  Copyright © 2019 Sun. All rights reserved.
//

#import "CustomTextView.h"

#define INT_LONG_BASE(x) ((long)x)

#define INT_ULONG_BASE(x) ((unsigned long)x)

@interface CustomTextView ()

@end

@implementation CustomTextView

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self viewdidload];
    }
    return self;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextViewTextDidChangeNotification object:self];

    [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextFieldTextDidBeginEditingNotification object:self];

    [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextFieldTextDidEndEditingNotification object:self];

    if (self.EditChangedBlock) {
        self.EditChangedBlock = nil;
    }
}



- (void)viewdidload{

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textViewEditChanged:) name:UITextViewTextDidChangeNotification object:self];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textViewEditBegin:) name:UITextViewTextDidBeginEditingNotification object:self];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textViewEditEnd:) name:UITextViewTextDidEndEditingNotification object:self];

    self.showsVerticalScrollIndicator = NO;
    _textLength = 10000;
    _interception = NO;
    _placehTextColor = [UIColor grayColor];
    _placehFont = [UIFont systemFontOfSize:14.0];
    _placehText = @"";
//    _promptLabHiden = YES;
//    _promptFrameMaxX = 10.0;
//    _promptFrameMaxY = 10.0;
//    _promptTextColor = [UIColor grayColor];
//    _promptFont = [UIFont systemFontOfSize:14.0];
//    _promptBackground = self.backgroundColor;
    _placehLab = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, self.frame.size.width-5, 0)];
    _placehLab.backgroundColor = [UIColor clearColor];
    _placehLab.textColor = _placehTextColor;
    _placehLab.font = _placehFont;
    _placehLab.numberOfLines = 0;
    [self addSubview:_placehLab];
//    if (_promptLabHiden) {//初始界面加入右下角字数提示语句，开始编辑时需要删除，因为textview_scrollview上加载的视图会跟随画布移动
//        _promptLab = [[UILabel alloc] init];
//        _promptLab.backgroundColor = [UIColor clearColor];
//        _promptLab.font = _promptFont;
//        _promptLab.textColor = _promptTextColor;
//        [self addSubview:_promptLab];
//    }
}

#pragma mark <--------------self_text_set-------------->
- (void)setTextLength:(NSInteger)textLength {
    _textLength = textLength;
//    _promptLab.text = [NSString stringWithFormat:@"0/%ld",_textLength];
//    [_promptLab sizeToFit];
//    _promptLab.frame = CGRectMake(CGRectGetWidth(self.frame)-CGRectGetWidth(_promptLab.frame)-_promptFrameMaxX, CGRectGetHeight(self.frame)-CGRectGetHeight(_promptLab.frame)-_promptFrameMaxY, CGRectGetWidth(_promptLab.frame), CGRectGetHeight(_promptLab.frame));
//    self.scrollEnabled = NO;
//    self.contentInset = UIEdgeInsetsMake(self.contentInset.top, self.contentInset.left, self.contentInset.bottom, self.contentInset.right);
}

#pragma mark <--------------placehLab_set-------------->
- (void)setPlacehTextColor:(UIColor *)placehTextColor {
    _placehTextColor = placehTextColor;
    _placehLab.textColor = _placehTextColor;
}

- (void)setPlacehFont:(UIFont *)placehFont {
    _placehFont = placehFont;
    _placehLab.font = _placehFont;
}

- (void)setPlacehText:(NSString *)placehText {
    _placehText = placehText;
//    if (!self.text.length) {
        _placehLab.text = _placehText;
        [_placehLab sizeToFit];
//    }else {
//        _placehLab.text = @"";
//        [_placehLab sizeToFit];
//    }
}

#pragma mark <--------------promptLab_set-------------->
//- (void)setPromptFrameMaxX:(CGFloat)promptFrameMaxX {
//    _promptFrameMaxX = promptFrameMaxX;
//    _promptLab.frame = CGRectMake(CGRectGetWidth(self.frame)-CGRectGetWidth(_promptLab.frame)-_promptFrameMaxX, CGRectGetHeight(self.frame)-CGRectGetHeight(_promptLab.frame)-_promptFrameMaxY, CGRectGetWidth(_promptLab.frame), CGRectGetHeight(_promptLab.frame));
//}
//
//- (void)setPromptFrameMaxY:(CGFloat)promptFrameMaxY {
//    _promptFrameMaxY = promptFrameMaxY;
//    _promptLab.frame = CGRectMake(CGRectGetWidth(self.frame)-CGRectGetWidth(_promptLab.frame)-_promptFrameMaxX, CGRectGetHeight(self.frame)-CGRectGetHeight(_promptLab.frame)-_promptFrameMaxY, CGRectGetWidth(_promptLab.frame), CGRectGetHeight(_promptLab.frame));
//}
//
//- (void)setPromptTextColor:(UIColor *)promptTextColor {
//    _promptTextColor = promptTextColor;
//    _promptLab.textColor = _promptTextColor;
//}
//
//- (void)setPromptFont:(UIFont *)promptFont {
//    _promptFont = promptFont;
//    _promptLab.font = _promptFont;
//    [_promptLab sizeToFit];
//    _promptLab.frame = CGRectMake(CGRectGetWidth(self.frame)-CGRectGetWidth(_promptLab.frame)-_promptFrameMaxX, CGRectGetHeight(self.frame)-CGRectGetHeight(_promptLab.frame)-_promptFrameMaxY, CGRectGetWidth(_promptLab.frame), CGRectGetHeight(_promptLab.frame));
//}
//
//- (void)setPromptBackground:(UIColor *)promptBackground {
//    _promptBackground = promptBackground;
//    _promptLab.backgroundColor = _promptBackground;
//}

#pragma mark <--------------textView_通知监听-------------->
- (void)textViewEditBegin:(NSNotification *)obj {//编辑开始
    self.scrollEnabled = YES;
//    if (_promptLabHiden && [[_promptLab superview] isKindOfClass:[self class]] && _promptLab) {
//        [_promptLab removeFromSuperview];//删除旧的初始界面右下角字数提示语句父视图是（self），开始编辑时需要删除，因为textview_scrollview上加载的视图会跟随画布移动
//        self.scrollEnabled = YES;
//        //新的右下角字数提示语句
//        _promptLab = [[UILabel alloc] init];
//        _promptLab.backgroundColor = self.backgroundColor;
//        _promptLab.font = _promptFont;
//        _promptLab.textColor = [UIColor grayColor];
//        _promptLab.text = [NSString stringWithFormat:@"%ld/%ld",self.text.length,_textLength];
//        [_promptLab sizeToFit];
//        _promptLab.frame = CGRectMake(CGRectGetMaxX(self.frame)-CGRectGetWidth(_promptLab.frame)-_promptFrameMaxX, CGRectGetMaxY(self.frame)-CGRectGetHeight(_promptLab.frame)-_promptFrameMaxY, CGRectGetWidth(_promptLab.frame), CGRectGetHeight(_promptLab.frame));
//        //底部的遮罩图
////        UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.frame) - CGRectGetHeight(_promptLab.frame) - _promptFrameMaxY, CGRectGetWidth(self.frame), CGRectGetHeight(_promptLab.frame) + _promptFrameMaxY)];
////        whiteView.backgroundColor = self.backgroundColor;
////        [[self superview] addSubview:whiteView];//父视图切换为[self superview],防止跟随付self.scrollview的画布滚动
//        [[self superview] addSubview:_promptLab];//父视图切换为[self superview],防止跟随付self.scrollview的画布滚动
//    }
}

- (void)textViewEditEnd:(NSNotification *)obj {//编辑结束
    
}

- (void)textViewEditChanged:(NSNotification *)obj {//编辑中
    if (self.text.length == 0) {
        _placehLab.hidden = NO;
    }else{
        _placehLab.hidden = YES;
    }
    NSString *toBeString = self.text;
    NSString *primaryLanguageStr = self.textInputMode.primaryLanguage; // 键盘输入模式
    if ([primaryLanguageStr isEqualToString:@"zh-Hans"]) {
        UITextRange *selectedRange = [self markedTextRange];
        //获取高亮部分
        UITextPosition *position = [self positionFromPosition:selectedRange.start offset:0];
        //没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position) {
            if (toBeString.length > _textLength) {
                if (_interception) {
                    self.text = [toBeString substringToIndex:_textLength];
                }else {
                    
                }
                [self changePromptLab];
            }else {
                [self changePromptLab];
            }
        }else {
            //有高亮选择的字符串，则暂不对文字进行统计和限制
        }
    }else {
        //中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
        if (toBeString.length > _textLength) {
            if (_interception) {
                self.text = [toBeString substringToIndex:_textLength];
            }else {
                
            }
            [self changePromptLab];
        }else {
            [self changePromptLab];
        }
    }
    if (self.EditChangedBlock) {//一个词语输出监听
        self.EditChangedBlock();
    }
}

- (void)changePromptLab {
//    NSString *changeStr = [NSString stringWithFormat:@"%ld/%ld",self.text.length,_textLength];
//    if (!_interception && (self.text.length > _textLength)) {
//        NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:changeStr];
//        [attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, changeStr.length - [NSString stringWithFormat:@"%ld",_textLength].length)];
//        _promptLab.attributedText = attributedStr;
//    }else {
//        _promptLab.text = changeStr;
//    }
//    [_promptLab sizeToFit];
//    _promptLab.frame = CGRectMake(CGRectGetMaxX(self.frame)-CGRectGetWidth(_promptLab.frame)-_promptFrameMaxX, CGRectGetMaxY(self.frame)-CGRectGetHeight(_promptLab.frame)-_promptFrameMaxY, CGRectGetWidth(_promptLab.frame), CGRectGetHeight(_promptLab.frame));
}

@end
